import modelExtend from 'dva-model-extend'
import {pageModel} from './common'
import { organizationList1,organizationInfo1,newOrganization1,
		updateOrganization1,deleteOrganization1,addOrganization1,
		allOrganizationlist1,addDepartment1,deleteDeparttment1,
		removeOrganization1} from 'services/organization'

export default modelExtend(pageModel,{
	namespace:'organization',
	state:{
		list:"",
		info:"",
		params:"",
		menu:0,
		visible:false,
		visible1:false,
		orginList:'',
		isNew:true,
		depId:'',
		pagination1:{
			showSizeChanger:true,
			showQuickJumper:true,
			showTotal:total => `共${total}条`,
			current:1,
			total:null,
		}
	},
	subscriptions:{
		setup({dispatch,history}){
			history.listen((location)=>{
				if(location.pathname == '/sys/organization')
					dispatch({
						type:'list',
						payload:location.query,
					})
			})
		}
	},
	effects:{
		*newOrganization({payload},{call,put}){
			const data = yield call(newOrganization1,payload)
			if(data){
				yield put({type:'reload'})
				yield put({type:'hideModal'})
			}
		},
		*addChildOrganization({payload},{call,put}){
			const data = yield call(addOrganization1,payload)
			if(data){
				yield put({type:'reload'})
				yield put({type:'hideModal'})
			}
		},
		*updateOrganization({payload},{call,put}){
			const data = yield call(updateOrganization1,payload)
			if(data){
				yield put({type:'reload'})
				yield put({type:'hideModal'})
			}
		},
		*delete({payload},{call,put}){
			const data = yield call(deleteOrganization1,payload)
			if(data){
				yield put({type:'reload'})
			}
		},
		*deleteDep({payload},{call,put}){
			const data = yield call(deleteDeparttment1,payload)
			if(data){
				yield put({type:'reload'})
			}
		},
		*remove({payload},{call,put}){
			const data = yield call(removeOrganization1,payload)
			if(data){
				yield put({type:'reload'})
			}
		},
		*reload(action, { put, select }) {
	      const page = yield select(({ organization }) => organization.pagination1)
	
	      yield put({ type: 'list', payload: { 
	          current:page.current
	       } });
	    },	
		*organList({payload},{call,put}){
			yield put({type:'showLoading'})
			const data = yield call(allOrganizationlist1,payload)
			if(data){
				yield put({type:'hideLoading'})
				yield put({
					type:'organSuccess',
					payload:{
						orginList:data.list
					}
				})
			}
		},
		*list({payload},{call,put}){
		 	const data = yield call(organizationList1,payload);
		 	if(data){
		 		yield put({
		 			type:'querySuccess',
			 		payload:{
			 			params:payload,
			 			list:data.page.list,
			 			pagination1:{
	    						total: data.page.totalCount,
	            				current: data.page.currPage
	    					}
			 		}
		 		})
		 	}
		},
		*info({payload},{call,put}){
			const data = yield call(organizationInfo1,{id:payload.id});
			if(data){
				yield put({type:'showModal',payload:payload.isNew})
				yield put({
					type:'infoSuccess',
					payload:{
						info:data.organization
					}
				})
			}
		},
		*addDep({payload},{call,put}){
			const data = yield call(addDepartment1,payload)
			console.log(data)
			if(data.success){
				yield put({type:'reload'})
				yield put({type:'hideAddModal'})
			}
		},
		*showModal({payload},{call,put}){
//			console.log(payload)
			yield put({
				type:'showModalSuccess',
				payload:{
					visible:true,
					isNew:payload,
				}
			})
		},
		*hideModal({payload},{call,put}){
			yield put({
				type:'hideModalSuccess',
				payload:{
					visible:false
				}
			})
		},
		*showAddModal({payload},{call,put}){
			console.log(payload)
			yield put({
				type:'showAddModalSuccess',
				payload:{
					visible1:true,
					depId:payload
				}
			})
		},
		*hideAddModal({payload},{call,put}){
			yield put({
				type:'hideAddModalSuccess',
				payload:{
					visible1:false
				}
			})
		},		
	},
	reducers:{
		querySuccess(state,action){
			const { list,params,pagination1 } = action.payload
			return {...state,
				list,
				params,
				pagination1:{
					...state.pagination1,
					...pagination1
				}
			}
		},
		showModalSuccess(state,action){ 
			const { visible,isNew } =  action.payload
			return {...state,
				visible,
				isNew
			}
		},
		hideModalSuccess(state,action){
			const { visible } = action.payload
			return{...state,
				visible
			}
		},
		showAddModalSuccess(state,action){ 
			const { visible1,depId } =  action.payload
			return {...state,
				visible1,
				depId,
			}
		},
		hideAddModalSuccess(state,action){
			const { visible1 } = action.payload
			return{...state,
				visible1,
			}
		},		
		infoSuccess(state,action){
			const { info } = action.payload
			return{...state,
				info
			}
		},
		organSuccess(state,action){
			const { orginList } = action.payload
			return{...state,
				orginList
			}
		},
	 	showLoading(state) {
	      	return {
	        		...state,
	        		loading: true
	      	};
   	    },
   	    hideLoading(state) {
	      	return {
	        		...state,
	        		loading: false
	      	};
   	    },
	},
})
